Skip to content

Add Model cache to cache model templates in memory#46

Merged
jmazanec15 merged 12 commits intoopensearch-project:feature/faiss-supportfrom
jmazanec15:model-cache
Jul 1, 2021
Merged

Add Model cache to cache model templates in memory#46
jmazanec15 merged 12 commits intoopensearch-project:feature/faiss-supportfrom
jmazanec15:model-cache

Conversation

@jmazanec15
Copy link
Copy Markdown
Member

Description

For OpenSearch indices that require training, a trained model template index will need to be created and stored in the Model system index before ingestion can begin. In order for the plugin to create a segment for indices that require training, a template will need to be loaded from the Model system index and passed to the jni. If the KNNDocValuesConsumer were to make a get request to this index for each segment creation operation, indexing will be slow. The purpose of this cache is to speed up this operation by storing the model template index in memory on a given node.

Cache size is determined by a new cluster setting that is updateable. On update, the cache will be rebuilt.

Additionally, this PR refactors ModelIndex into ModelDao interface. This helps for testing. However, no core functionality of the model index has been changed.

Unit tests for the cache have been added to validate.

This PR does not introduce APIs or functionality to warmup or remove entries from the cache. In the future, we can investigate adding these APIs and functionality.

Issues Resolved

#27

Check List

  • New functionality includes testing.
    • All tests pass
  • New functionality has been documented.
    • New functionality has javadoc added
  • Commits are signed as per the DCO using --signoff

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

Signed-off-by: John Mazanec <jmazane@amazon.com>
Signed-off-by: John Mazanec <jmazane@amazon.com>
Signed-off-by: John Mazanec <jmazane@amazon.com>
(s) -> Long.toString(KNN_DEFAULT_MODEL_CACHE_SIZE_IN_BYTES),
(s) -> {
long value = Long.parseLong(s);
if (value < KNN_MIN_MODEL_CACHE_SIZE_IN_BYTES) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: why not combine both into single check and error message as "value must be > 100 KB and <= 80 MB, so that user knows required setting at first failure attempt?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure will update


private static Logger logger = LogManager.getLogger(ModelCache.class);

private static ModelCache INSTANCE;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why Upper case? i believe only constants are in Upper case

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, will update

Signed-off-by: John Mazanec <jmazane@amazon.com>
@jmazanec15 jmazanec15 changed the base branch from faiss-develop to feature/faiss-support June 24, 2021 17:00
@jmazanec15 jmazanec15 requested a review from vamshin June 28, 2021 16:26
@VijayanB VijayanB added the Enhancements Increases software capabilities beyond original client specifications label Jun 29, 2021
Comment thread src/main/java/org/opensearch/knn/indices/ModelCache.java
Comment thread src/main/java/org/opensearch/knn/indices/ModelDao.java
Copy link
Copy Markdown
Member

@VijayanB VijayanB left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for answers. it looks good. Will keep it as it is.

Comment thread src/main/java/org/opensearch/knn/index/KNNSettings.java Outdated
Comment thread src/main/java/org/opensearch/knn/index/KNNSettings.java Outdated
@Override
public void delete(String modelId, ActionListener<DeleteResponse> listener) {
if (!isCreated()) {
throw new IllegalStateException("Cannot delete model \"" + modelId + "\". Model index does not exist.");
Copy link
Copy Markdown
Member

@vamshin vamshin Jun 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the Model index is deleted which means model is deleted right? Why should this be an exception? May be mark as logger.info for debugging purpose?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My thinking for throwing an exception is that a user should not try to delete something that isnt there. I can switch to a log statement and return.

Btw, this function is subject to change. For instance, what happens when someone tries to delete a model that is in use by an index? This could cause problems. I will need to think it through a little more once I start implementing Model Index management APIs.

Comment thread src/main/java/org/opensearch/knn/indices/ModelDao.java Outdated
Comment thread src/main/java/org/opensearch/knn/indices/ModelDao.java Outdated
Signed-off-by: John Mazanec <jmazane@amazon.com>
Signed-off-by: John Mazanec <jmazane@amazon.com>
Signed-off-by: John Mazanec <jmazane@amazon.com>
Signed-off-by: John Mazanec <jmazane@amazon.com>
Signed-off-by: John Mazanec <jmazane@amazon.com>
Signed-off-by: John Mazanec <jmazane@amazon.com>
@Override
public void put(String modelId, KNNEngine knnEngine, byte[] modelBlob, ActionListener<IndexResponse> listener) {
if (!isCreated()) {
throw new IllegalStateException("Cannot put model in index before index has been initialized");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: Is it possible Model index not created and we end up with this put? If its the case, why not we create Model index on the first put and proceed?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is possible. I think that makes sense. Will update.

Signed-off-by: John Mazanec <jmazane@amazon.com>
Copy link
Copy Markdown
Member

@vamshin vamshin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks

@jmazanec15 jmazanec15 merged commit e4f42ff into opensearch-project:feature/faiss-support Jul 1, 2021
jmazanec15 added a commit to jmazanec15/k-NN-1 that referenced this pull request Oct 22, 2021
jmazanec15 added a commit that referenced this pull request Oct 22, 2021
Signed-off-by: Jack Mazanec <jmazane@amazon.com>
martin-gaievski pushed a commit to martin-gaievski/k-NN that referenced this pull request Mar 7, 2022
martin-gaievski pushed a commit to martin-gaievski/k-NN that referenced this pull request Mar 7, 2022
…t#46)

Signed-off-by: Jack Mazanec <jmazane@amazon.com>
Signed-off-by: Martin Gaievski <gaievski@amazon.com>
martin-gaievski pushed a commit to martin-gaievski/k-NN that referenced this pull request Mar 30, 2022
jingqimao77-spec pushed a commit to jingqimao77-spec/k-NN that referenced this pull request Mar 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Enhancements Increases software capabilities beyond original client specifications

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants